package com.financialDog.javaTool.controller.fileManage;

import com.financialDog.javaTool.config.ConfigManager;
import com.financialDog.javaTool.controller.reportTools.ReportMergeToolController;
import com.financialDog.javaTool.reportModel.ConfigVo;
import com.financialDog.javaTool.view.fileManager.ConfigManageView;
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
import com.xwintop.xcore.util.javafx.TooltipUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.control.Button;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

public class ConfigManageController extends ConfigManageView {

    public static final String FXML = "/com/financialDog/javaTool/fxmlView/reportTools/ConfigManage.fxml";

    private ReportMergeToolController reportMergeToolController;

    private ConfigManager configManager = ConfigManager.getInstance();

    private ObservableList<Map<String, String>> tableData = FXCollections.observableArrayList();

    public static FXMLLoader getFXMLLoader() {
           return new FXMLLoader(ConfigManageController.class.getResource(FXML));
    }

    private String[] filtrationTypeChoiceBoxStrings = new String[] { "所有","资产负债表","现金流量表", "利润表"};

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initView();
        initEvent();
        initService();
    }

    private void initView() {
        //filtrationTypeChoiceBox.getItems().addAll(filtrationTypeChoiceBoxStrings);
        JavaFxViewUtil.setTableColumnMapValueFactory(nameTableColumn, "name");
        JavaFxViewUtil.setTableColumnMapValueFactory(startTimeTableColumn, "createTime");
        JavaFxViewUtil.setTableColumnMapValueFactory(updateTimeTableColumn, "updateTime");
        JavaFxViewUtil.setTableColumnMapValueFactory(operateTableColumn, "operate");

        configManager.generateConfigTableData(tableData);
        configManageTableView.setItems(tableData);
    }

    private void initEvent() {

    }

    private void initService() {
        //pluginManageService.getPluginList();
    }

    public void setReportMergeToolController(ReportMergeToolController reportMergeToolController){
        this.reportMergeToolController = reportMergeToolController;

        //初始化数据
	    initData();
    }

    @FXML
    private void configAddAction(ActionEvent event) {
        final Stage dialog = new Stage();
        dialog.setTitle("项目添加");
        dialog.initModality(Modality.APPLICATION_MODAL);
        Button addButton = new Button("确认添加");

        TextField projectName = new TextField();
        VBox dialogVbox = new VBox(20);
        dialogVbox.getChildren().add( new Label("项目名称"));
        dialogVbox.getChildren().add( projectName);
        dialogVbox.getChildren().add(addButton);
        Scene dialogScene = new Scene(dialogVbox, 300, 200);

        addButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                String projectValue = projectName.getText();
                if(!configManager.checkNameForProject(projectValue)){
                    TooltipUtil.showToast("项目名称已经被使用, 请换一种项目");
                }
                configManager.addNewProject(projectValue);
                updateTableView(configManager.getCurrentConfig());
                dialog.hide();
            }
        });

        dialog.setScene(dialogScene);
        dialog.show();
    }


    private void updateTableView(ConfigVo configVo){
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("name", configVo.getProjectName());
        hashMap.put("createTime", configVo.getCreateDate());
        hashMap.put("updateTime",  configVo.getUpdateDate());
        tableData.add(hashMap);
    }

    private void initData(){
    }

}
